package com.dsp.web;

import java.math.BigDecimal; 
import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.core.util.DspUtil;
import com.core.util.Pagination;
import com.dsp.bean.Message;
import com.dsp.bean.PersonalAccount;
import com.dsp.services.PersonalAccountService;

@Controller("PersonalAccountController")
public class PersonalAccountController extends DspController<PersonalAccount> {
	
	public static String[] QueryColumn={"province","city","area","hukou","itemId","personalType"};
	public static String[] QueryType = {"String","String","String","Integer","Integer","Integer"};
	
	public PersonalAccountController() {
		super(PersonalAccount.class);
	}

	Logger logger = Logger.getLogger(DspUtil.Log4J);

	@Autowired
	private PersonalAccountService personalAccountService;

	@RequestMapping(value = "/personal_accounts", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("personal_account/list");
		mav.addObject("path", "personal_account/list.jsp");
		mav.addObject("current", "personal_accounts");
		return mav;
	}

	@RequestMapping(value = "/personal_accounts_list")
	@ResponseBody
	public Pagination<PersonalAccount> index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		super.initHttp(request, response);
		DetachedCriteria dc = DetachedCriteria.forClass(PersonalAccount.class);
		super.getPageParameters(dc, QueryColumn, QueryType);
		Pagination<PersonalAccount> p = this.personalAccountService.search(dc, (page - 1) * rp, rp);
		p.setPage(page);
		return p;
	}

	@RequestMapping(value = "/personal_accounts_create", method = RequestMethod.POST)
	@ResponseBody
	public Message create(@RequestBody PersonalAccount personalAccount) {
		Message message = new Message();
		logger.info("正在创建用户信息");
		try {
			if(personalAccount.getPersonalLimit() == null){
				personalAccount.setPersonalLimit(new BigDecimal(99999));
			}
			if(personalAccount.getPersonalLower() == null){
				personalAccount.setPersonalLower(new BigDecimal(0));
			}
			if(personalAccount.getPersonalType() == 2) {
				personalAccount.setRate(null);
			} else {
				personalAccount.setFixValue(null);
			}
			if (personalAccount.getId() != null) {
				this.personalAccountService.update(personalAccount);
			} else {
				this.personalAccountService.save(personalAccount);
			}
		} catch (Exception se) {
			se.getStackTrace();
			message.setSuccess(false);
			List<String> msg = new ArrayList<String>();
			msg.add(se.getLocalizedMessage());
			message.setMsg(msg);
		}
		logger.info("完成创建信息");
		return message;
	}

	@RequestMapping(value = "/personal_accounts_edit", method = RequestMethod.POST)
	@ResponseBody
	public PersonalAccount edit(@RequestBody PersonalAccount _personalAccount) {
		PersonalAccount personalAccount = this.personalAccountService.findById(_personalAccount.getId());
		return personalAccount;
	}

	@RequestMapping(value = "/personal_accounts_delete", method = RequestMethod.POST)
	@ResponseBody
	public Message delete(@RequestBody PersonalAccount personalAccount) {
		Message message = new Message();
		String msg = this.personalAccountService.delete(personalAccount);
		message.setSuccess(Boolean.valueOf(msg));
		return message;
	}

	/**
	 * @return the personalAccountService
	 */
	public PersonalAccountService getPersonalAccountService() {
		return personalAccountService;
	}

	/**
	 * @param personalAccountService
	 *            the personalAccountService to set
	 */
	public void setPersonalAccountService(PersonalAccountService personalAccountService) {
		this.personalAccountService = personalAccountService;
	}

}
